/**
 * 后台JS主入口
 */

layui.define(['form', 'layer', 'element', 'laydate'], function(exports) {
	var layer = layui.layer,
		element = layui.element,
		laydate = layui.laydate,
		form = layui.form;

	/**
	 * AJAX全局设置
	 */
	$.ajaxSetup({
		type: "post",
		dataType: "json"
	});

	/**
	 * 后台侧边菜单选中状态
	 */
	$('.layui-nav-item').find('a').removeClass('layui-this');
	$('.layui-nav-tree').find('a[href*="admin/' + GV.current_controller + '"]').parent().addClass('layui-this').parents('.layui-nav-item').addClass('layui-nav-itemed');

	/**
	 * 通用日期时间选择
	 */
	laydate.render({
		elem: '#datetime', //指定元素
		type: 'datetime'
	});

	/**
	 * 通用表单提交(AJAX方式)
	 */
	form.on('submit(*)', function(data) {
		$.ajax({
			url: data.form.action,
			type: data.form.method,
			data: data.field,
			success: function(info) {
				if(info.code === 1) {
					setTimeout(function() {
						location.href = info.url;
					}, 1000);
				}
				layer.msg(info.msg);
			}
		});

		return false;
	});

	/**
	 * 全选
	 */
	form.on('checkbox(allChoose)', function(data) {
		var child = $(data.elem).parents('table').find('tbody input.checkbox-ids');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	/**
	 * 通用Ajax
	 */
	$('.ajax-action').on('click', function() {
		var _href = $(this).attr('href');
		$.ajax({
			url: _href,
			type: "post",
			success: function(info) {
				if(info.code === 1) {
					setTimeout(function() {
						location.href = info.url;
					}, 1000);
				}
				layer.msg(info.msg);
			}
		});
	});

	/**
	 * 通用Ajax
	 */
	$('.ajax-do').on('click', function() {
		var _href = $(this).attr('href');
		layer.open({
			shade: false,
			content: '确定操作？',
			btn: ['确定', '取消'],
			yes: function(index) {
				$.ajax({
					url: _href,
					type: "get",
					success: function(info) {
						if(info.code === 1) {
							setTimeout(function() {
								location.href = info.url;
							}, 1000);
						}
						layer.msg(info.msg);
					}
				});
				layer.close(index);
			}
		});
		return false;
	});
		/**
	 * 通用列表组
	 */
	$('.confirm').click(function() {
		var that = $(this),
			code = function(that) {
				var href = that.attr('ref') ? that.attr('ref') : that.attr('data-href');
				$.post(href, '', function(res) {
					layer.msg(res.msg, {
						time: 1000
					}, function() {
						if(res.code != 0) {
							location.reload();
						}
					});
				});
			};
		if(that.hasClass('confirm')) {
			var tips = that.prop('title') ? that.prop('title') : '您确定要操作吗？';
			layer.confirm(tips, {
				title: false,
				closeBtn: 0
			}, function(index) {
				code(that);
				layer.close(index);
			});
		} else {
			code(that);
		}
		return false;
	});
	$(".search_btn").click(function(){
		$(".layui-form")[0].submit();
	})
	/**
	 * 通用列表组
	 */
	$('.page-btns').click(function() {
		var that = $(this),
			code = function(that) {
				var href = that.attr('href') ? that.attr('href') : that.attr('data-href');
				if(!href) {
					layer.msg('请设置data-href参数');
					return false;
				}
				if($('.checkbox-ids:checked').length <= 0) {
					layer.msg('请选择要操作的数据');
					return false;
				}
				var query = $('#listForm').serialize();
				$.post(href, query, function(res) {
					layer.msg(res.msg, {
						time: 1000
					}, function() {
						if(res.code != 0) {
							location.reload();
						}
					});
				});
			};
		if(that.hasClass('confirm')) {
			var tips = that.prop('title') ? that.prop('title') : '您确定要操作吗？';
			layer.confirm(tips, {
				title: false,
				closeBtn: 0
			}, function(index) {
				code(that);
				layer.close(index);
			});
		} else {
			code(that);
		}
		return false;
	});

	/**
	 * 更新排序
	 * */
	$('.input-sort').focusout(function() {
		var that = $(this),
			_val = that.val();
		if(_val == '') return false;
		if(that.attr('data-value') == _val) return false;
		if(!that.attr('data-href')) {
			layer.msg('请设置data-href参数');
			return false;
		}
		$.post(that.attr('data-href'), {
			val: _val
		}, function(res) {
			if(res.code == 1) {
				that.attr('data-value', _val);
			}
			layer.msg(res.msg);
		});
	});

	/* 监听状态设置开关 */
	form.on('switch(switchStatus)', function(data) {
		var that = $(this),
			status = 0;
		if(!that.attr('data-href')) {
			layer.msg('请设置data-href参数');
			return false;
		}
		if(this.checked) {
			status = 1;
		}
		$.get(that.attr('data-href'), {
			val: status
		}, function(res) {
			layer.msg(res.msg);
			if(res.code == 0) {
				that.trigger('click');
				form.render('checkbox');
			}
		});
	});

	/**
	 * 清除缓存
	 */
	$('#clear-cache').on('click', function(event) {
		event.preventDefault();
		var _url = $(this).data('url');
		if(_url !== 'undefined') {
			$.ajax({
				url: _url,
				success: function(data) {
					if(data.code === 1) {
						setTimeout(function() {
							location.href = location.pathname;
						}, 1000);
					}
					layer.msg(data.msg);
				}
			});
		}

		return false;
	});

	exports('admin', {});
});